package tichu_project252.View.Player;

import java.awt.Color;
import java.awt.Insets;
import java.awt.Point;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import javax.swing.JComponent;
import tichu_project252.CardClasses.Card;
import tichu_project252.Model.Model;

public class CardContainer extends JComponent {

    private LinkedList<PlayerCards> container = null;
    private OverlapLayout layout = null;
    private Model m = null;

    /**
     *  We use for the instant creation of the cards.
     * 
     * <p><b>Constructor: </b> CardContainer(Model m, int n, int size)  </p>
     *
     * <p><b>Precondition: </b> </p> <p><b>Postcondition: </b> Constructs a new
     * Jcomponent </p>
     * 
     * @param m The model
     * @param n The player
     * @param size how far we want our cards to be
     * @throws IOException if an error occurs
     */
    
    public CardContainer(Model m, int n, int size) throws IOException {
        super();
        layout = new OverlapLayout(new Point(size, 0));
        layout.setPopupInsets(new Insets(20, 0, 0, 0));
        this.setLayout(layout);
        this.setBackground(Color.DARK_GRAY);
       
        this.m = m;
        init(n);
        this.setVisible(true);
    }

    /**
     *  We use it for fill later the container with cards.
     * 
     * <p><b>Constructor: </b> CardContainer(Model m, int n, int size)  </p>
     *
     * <p><b>Precondition: </b> </p> <p><b>Postcondition: </b> Constructs a new
     * Jcomponent </p>
     * 
     * @param m The model
     * @param size how far we want our cards to be
     * @throws IOException if an error occurs
     */
    
    public CardContainer(Model m, int size) throws IOException {
        super();
        layout = new OverlapLayout(new Point(size, 0));
        layout.setPopupInsets(new Insets(20, 0, 0, 0));
        this.setLayout(layout);
        this.setBackground(Color.DARK_GRAY);
        this.setVisible(true);
        this.m = m;
        container = new LinkedList<PlayerCards>();
    }
/**
 * Add the cards int he container.
 * @param card
 * @throws IOException 
 */
    public void addAll(LinkedList<Card> card) throws IOException {
        Collections.sort(card, new CardComparator());
        for (int i = 0; i < card.size(); i++) {
            container.add(new PlayerCards(m, card.get(i)));
        }

        for (int i = 0; i < container.size(); i++) {
            this.add(container.get(i));
        }
        this.invalidate();
        this.validate();
    }
/**
 * Comparison between two cards.
 */
    class CardComparator implements Comparator {

        public int compare(Object o1, Object o2) {
            Card c1 = (Card) o1;
            Card c2 = (Card) o2;

            if (c1.getintRepr() > c2.getintRepr()) {
                return 1;
            } else if (c1.getintRepr() == c2.getintRepr()) {
                return 0;
            } else {
                return -1;
            }
        }
    }
/**
 * Initialize the container.
 * @param n The player 
 * @throws IOException 
 */
    private void init(int n) throws IOException {
        LinkedList<Card> card = m.getPlayer(n).getCards();

        Collections.sort(card, new CardComparator());
        container = new LinkedList<PlayerCards>();

        System.out.println(card.size());

        for (int i = 0; i < card.size(); i++) {
            System.out.println(card.get(i).getImage());
            container.add(new PlayerCards(m, card.get(i)));
        }

        for (int i = 0; i < container.size(); i++) {
            this.add(container.get(i));
        }

    }

    /**
     * Cleans the container.
     */
    public void cleanup() {
        for (int i = 0; i < container.size(); i++) {
            if (container.get(i).getCard().getSelected() == true) {
                this.remove(container.get(i));
            }
        }

        LinkedList<PlayerCards> temp = new LinkedList<PlayerCards>();

        for (int i = 0; i < container.size(); i++) {
            if (container.get(i).getCard().getSelected() == true) {
                temp.add(container.get(i));
            }
        }
        container.removeAll(temp);
    }

    @Override
    public boolean isOptimizedDrawingEnabled() {
        return false;
    }
/**
 * Remove all elements from the container.
 */
    public void clear() {
        this.container.clear();
        this.removeAll();
    }
}
